package com.guo.core.framework.pro;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MileageTest {

    public static final String SRC = "D:/MyData/guozl22/IdeaProjects/tms-third/tms-third-service/src/main/resources/MileageTest.txt";

    public static final String SQL1 = "select mileage,line_code from logistics_otp_dispatch.transport where task_no =?;";

    public static final String SQL2 = "select * from logistics_otp_dispatch.trace t  where t.task_no =? and operate_type = 160";

    // dispatch-converged
    public static final String URL = "http://10.18.60.229:10121/transport/lineMatching?taskNo=";


    @Test
    public void test() throws IOException {
        List<String> list = FileUtils.readLines(new File(SRC), "UTF-8");

        StringBuffer es = new StringBuffer("'");
        list.forEach(param -> {
            try {
                Entity entity = Db.use("prod").queryOne(SQL1, param);
                if (!StringUtils.isEmpty(entity.getStr("mileage"))) {
                    Console.log("{}-里程存在-{}",param, JSONUtil.toJsonStr(entity));
                    return;
                }

                List<Entity> resultList = Db.use("prod").query(SQL2, param);
                if (CollectionUtil.isEmpty(resultList)) {
                    Console.log("{}-无trace",param);
                    return;
                }

                boolean istimeout = resultList.stream().anyMatch(e ->
                    e.getStr("remark").contains("{\"code\":\"500\"")
                        || e.getStr("remark").contains("{\"code\":\"1\"")
                        || e.getStr("remark").contains("{\"code\":\"-1\""));
                if (!istimeout) {
                    Console.log("{}-正常",param);
                    return;
                }

//                Console.log("{}-异常-{}",param, JSONUtil.toJsonStr(resultList));
                String result = HttpUtil.get(URL.concat(param));
                Console.log("{}-返回-{}",param, result);

                es.append(param).append("','");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });

        Console.log(es.toString());
    }

    @Test
    public void queryMengJing() throws IOException, SQLException {
        List<City> list = new ArrayList<>();
        init(list);

        list.forEach(city -> {
            Console.log(city.getS() + " -> " + city.getD() + " " + city.getD_code());
            });

        list.forEach(city -> {
            try {
                this.installSql(city.getS(), city.getD(), city.getD_code());
            } catch (Exception e) {
                Console.error("异常了。", e);
            }
        });


    }

    private void init(List<City> list) {
        City c1 = new City();
        c1.setS("1350427");
        c1.setD("沙县区");
        c1.setD_code("1350405");
        list.add(c1);
        City c2 = new City();
        c2.setS("1350625");
        c2.setD("长泰区");
        c2.setD_code("1350605");
        list.add(c2);
        City c3 = new City();
        c3.setS("1520221");
        c3.setD("水城区");
        c3.setD_code("1520204");
        list.add(c3);
        City c4 = new City();
        c4.setS("1431121");
        c4.setD("祁阳市");
        c4.setD_code("1431181");
        list.add(c4);
        City c5 = new City();
        c5.setS("1350403");
        c5.setD("三元区");
        c5.setD_code("1350404");
        list.add(c5);
        City c6 = new City();
        c6.setS("1350402");
        c6.setD("三元区");
        c6.setD_code("1350404");
        list.add(c6);
        City c7 = new City();
        c7.setS("1620201");
        c7.setD("嘉峪关市");
        c7.setD_code("1620200");
        list.add(c7);
        City c8 = new City();
        c8.setS("1410381");
        c8.setD("偃师区");
        c8.setD_code("1410307");
        list.add(c8);

        City c9 = new City();
        c9.setS("1350681");
        c9.setD("龙海区");
        c9.setD_code("1350604");
        list.add(c9);
        City c10 = new City();
        c10.setS("1520522");
        c10.setD("黔西市");
        c10.setD_code("1520581");
        list.add(c10);
        City c11 = new City();
        c11.setS("1421023");
        c11.setD("监利市");
        c11.setD_code("1421088");
        list.add(c11);

        City c12 = new City();
        c12.setS("1340211");
        c12.setD("繁昌区");
        c12.setD_code("1340212");
        list.add(c12);

        City c13 = new City();
        c13.setS("1320684");
        c13.setD("海门区");
        c13.setD_code("1320614");
        list.add(c13);

        City c14 = new City();
        c14.setS("1410322");
        c14.setD("孟津区");
        c14.setD_code("1410308");
        list.add(c14);

    }

    private void installSql(String s,String d,String d_code) throws Exception {
        List<String> list = FileUtils.readLines(new File(SRC), "UTF-8");

        StringBuffer es = new StringBuffer("'");
        list.forEach(param -> {
            es.append(param).append("','");
        });

        es.delete(es.length()-2, es.length());

        StringBuffer updateSql = new StringBuffer();
//        String s = "黔西县";
//        String d = "黔西市";
//        String d_code = "1520581";
        String SQL4 = "select id,sender_district_code,sender_district_name,receiver_district_code,receiver_district_name from transport t where task_no in ("+es.toString()+") and (sender_district_code = '"+s+"' or receiver_district_code = '"+s+"') and mileage is null;";
        List<Entity> resultList = Db.use("prod").query(SQL4);
        resultList.forEach(entity->{
            String sender_district_code = entity.getStr("sender_district_code");
            String receiver_district_code = entity.getStr("receiver_district_code");
            if (!s.equals(sender_district_code) && !s.equals(receiver_district_code)) {
                return ;
            }

            updateSql.append("update transport set ");
            if (s.equals(sender_district_code) && !s.equals(receiver_district_code)) {
                updateSql.append("sender_district_code='"+d_code+"', sender_district_name='"+d+"' ");
            }

            if (s.equals(receiver_district_code) && !s.equals(sender_district_code)) {
                updateSql.append("receiver_district_code='"+d_code+"', receiver_district_name='"+d+"' ");
            }

            if (s.equals(receiver_district_code) && s.equals(sender_district_code)) {
                updateSql.append("sender_district_code='"+d_code+"', sender_district_name='"+d+"',receiver_district_code='"+d_code+"', receiver_district_name='"+d+"' ");
            }

            updateSql.append("where id='").append(entity.getStr("id")).append("';\n");

        });

        Console.log(updateSql.toString());
    }

    @Data
    class City {
        String s ;
        String d ;
        String d_code ;
    }

}
